<?php
/* ******************************
 *	Request aJax actions
 * ******************************
*/

require_once ('../../../includes/classes/core.php');
include('../../../includes/classes/log.class.php');
$log 		= new log();
$action = $_REQUEST['act'];
$error	= '';
$data	= '';


$task_id				= $_REQUEST['id'];
$call_date				= $_REQUEST['call_date'];
$control_id				= $_REQUEST['control_id'];
$phone					= $_REQUEST['phone'];
$persons_id				= $_REQUEST['persons_id'];
$call_duration 			= $_REQUEST['call_duration'];
$planned_start_date 	= $_REQUEST['planned_start_date'];
$fact_end_date 			= $_REQUEST['fact_end_date'];
$priority_id			= $_REQUEST['priority_id'];
$template_id			= $_REQUEST['template_id'];

// site_user
$personal_pin			= $_REQUEST['personal_pin'];
$personal_id			= $_REQUEST['personal_id'];
$personal_phone			= $_REQUEST['personal_phone'];
$mail				    = $_REQUEST['mail'];
$name				    = $_REQUEST['name'];
$friend_pin				= $_REQUEST['friend_pin'];

$problem_comment 		= $_REQUEST['problem_comment'];
$comment 	        	= $_REQUEST['comment'];
$hidden_inc				= $_REQUEST['hidden_inc'];
$edit_id				= $_REQUEST['edit_id'];
$delete_id				= $_REQUEST['delete_id'];

// file
$rand_file				= $_REQUEST['rand_file'];
$file					= $_REQUEST['file_name'];



switch ($action) {
	case 'get_add_page':
		$number		= $_REQUEST['number'];
		$page		= GetPage($res='', $number);
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	   
		$page		= GetPage(Getincomming($task_id));
        
        $data		= array('page'	=> $page);
        
        break;
	
 	case 'get_list' :
		$count		= $_REQUEST['count'];
	   	$hidden		= $_REQUEST['hidden'];
		$user_id	= $_SESSION['USERID'];
		$user		= $_SESSION['USERID'];
		$group		= checkgroup($user);
		
		$filter = '';
		if ($group != 2 && $group != 3) {
			$res_row = mysql_fetch_assoc(mysql_query("SELECT 	crystal_users.id as person_id
														FROM 	`crystal_users`
														WHERE 	`crystal_users`.`id` = $user_id"));
			
			$filter = 'AND (task.responsible_user_id ='.$res_row[person_id].' or task.control_user_id ='. $res_row[person_id].')';
		}
			
    	$rResult = mysql_query("SELECT		`task`.id,
    										`task`.id,
    										DATE_FORMAT(asterisk_outgoing.`call_datetime`,'%d/%m/%y <span style=\"font-weight: bold;\">%H:%i:%s</span>'),
    										if(ISNULL(task.incomming_call_id), task.`phone`, incomming_call.`phone`) AS datee,
    										incomming_call.pid AS personal_id,
											incomming_call.pin AS personal_pin,
    										if(ISNULL(task.incomming_call_id), source.`name`, sou.`name` ) AS source,
    										if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` ) AS category,
    										if(ISNULL(task.incomming_call_id), object.`name`, obj.`name` ) AS object,
    										`crystal_users`.`name` ,
    										`crystal_users1`.`name` ,
    										`status`.`call_status`,
    										TIME_FORMAT(SEC_TO_TIME(asterisk_outgoing.duration), '%i:%s')AS `duration`,
											CONCAT('<button class=\'download1\' str=',asterisk_outgoing.file_name,'>გამავალი</button>') AS `file_name`
                                FROM 		`task`
                                LEFT JOIN 	incomming_call ON task.incomming_call_id=incomming_call.id
                                LEFT JOIN 	`category` 		ON task.subcategory_id=category.id
                                LEFT JOIN 	`category` AS cat 		ON incomming_call.call_category_id=cat.id
                                LEFT JOIN 	`object` 		ON task.object_id=object.id
                                LEFT JOIN 	`object` AS obj 		ON incomming_call.object_id=obj.id
                              	LEFT JOIN 	source	AS sou	ON incomming_call.source_id=sou.id
                                LEFT JOIN 	source ON task.task_source_id=source.id
                               	LEFT JOIN 		crystal_users ON task.responsible_user_id=crystal_users.id	
                            	LEFT JOIN 		crystal_users   AS `crystal_users1`	ON incomming_call.user_id=crystal_users1.id
                                LEFT JOIN 	`status`  	ON	task.`status`=`status`.`id`
    							LEFT JOIN asterisk_outgoing ON task.uniqueid=asterisk_outgoing.uniqueid
								WHERE 		task.actived=1 and task.task_type_id = 1 and (task.status=0 or task.status=2) $filter
    							");
		    
		$data = array(
			"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			
			$row = array();
			for ( $i = 0 ; $i < $count; $i++ )
			{
				if($i==11){
				if 	($aRow[$i]=='გადაცემულია გასარკვევად') 			{	$row[] = '<div style="background: #FF5555; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='გარკვევის პროცესშია') 	{	$row[] = '<div style="background:#FFFF55; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='მოგვარებულია, აცნობეთ') 	{	$row[] = '<div style="background: #B1EF87; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='დასრულებული') 	{	$row[] = '<div style="background: #33dd33; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 else	{ $row[] = $aRow[$i];}
				
				}else	{ $row[] = $aRow[$i];}
				if($i == ($count - 1)){
					$row[] ='<input type="checkbox" id="' . $aRow[$hidden] . '" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

        break;
    case 'save_outgoing':
	
		$user_id		= $_SESSION['USERID'];
		
		if($task_id==0){
			$task_id = mysql_insert_id();
			Addtask( $persons_id,$control_id, $planned_start_date, $fact_end_date,$call_duration,  $priority_id, $template_id, $phone, $comment, $problem_comment,$file,$rand_file,$hidden_inc);
			if($personal_pin != 0){
				$incomming_call_id = mysql_insert_id();
				//Addsite_user($incomming_call_id, $personal_pin, $friend_pin, $personal_id);
				}
		}else{
			
			Savetask($task_id, $persons_id,$control_id, $planned_start_date, $fact_end_date,$call_duration,  $priority_id, $template_id, $comment, $problem_comment,$file,$rand_file);
			//Savesite_user($incom_id, $personal_pin, $name, $personal_phone, $mail,  $personal_id);
			
		}
		
        break;
        
    case 'get_responsible_person_add_page':
        $page 		= GetResoniblePersonPage();
        $data		= array('page'	=> $page);
        
        break;
        
    case 'change_responsible_person':
        $letters 			= json_decode( '['.$_REQUEST['lt'].']' );
        $responsible_person = $_REQUEST['rp'];
        
        ChangeResponsiblePerson($letters, $responsible_person);
        
        break;
    case 'get_add_info':
        
        $pin	=	$_REQUEST['pin'];
        $data 	= 	array('info' => get_addition_all_info($pin));
        
        break;
    case 'get_add_info1':
        
        $personal_id	=	$_REQUEST['personal_id'];
        $data 	= 	array('info1' => get_addition_all_info1($personal_id));
        
        break;
        
    case 'delete_file':
        
        	mysql_query("	DELETE 
				        	FROM file
				        	WHERE id = $delete_id");
				        
        	$increm = mysql_query("	SELECT  `name`,
        			`rand_name`,
        			`id`
        			FROM 	`file`
        			WHERE   `task_id` = $edit_id
        			");
        
        	$data1 = '';
        
        	while($increm_row = mysql_fetch_assoc($increm))	{
        	$data1 .='<tr style="border-bottom: 1px solid #85b1de;">
				          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>
				          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
        				          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
 					  </tr>';
		}
        
		$data = array('page' => $data1);
        
        		break;
        
        		case 'up_now':
        		$user		= $_SESSION['USERID'];
        		if($rand_file != ''){
        		mysql_query("INSERT INTO 	`file`
						        		( 	`user_id`,
						        		`task_id`,
						        		`name`,
						        		`rand_name`
						        		)
        						VALUES
						        		(	'$user',
						        		'$edit_id',
						        		'$file',
						        		'$rand_file'
						        		);");
						        		}
        
        		$increm = mysql_query("	SELECT  `name`,
								        		`rand_name`,
								        		`id`
						        		FROM 	`file`
						        		WHERE   `task_id` = $edit_id
								        		");
        
        		$data1 = '';
        
        		while($increm_row = mysql_fetch_assoc($increm))	{
        		$data1 .='<tr style="border-bottom: 1px solid #85b1de;">
        		<td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>
        		<td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
				          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
        				          </tr>';
		}
        
		$data = array('page' => $data1);
		
		break;
        
    case '':
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Request Functions
 * ******************************
 */

function checkgroup($user){
	$res = mysql_fetch_assoc(mysql_query("
											SELECT crystal_users.group_id
											FROM    crystal_users
											WHERE  crystal_users.id = $user
										"));
	return $res['group_id'];
	
}


function Addtask( $persons_id, $control_id, $planned_start_date, $fact_end_date,$call_duration,  $priority_id, $phone, $comment, $problem_comment,$file,$rand_file,$hidden_inc)
{  
	$c_date		= date('Y-m-d H:i:s');
	$user		= $_SESSION['USERID'];

	mysql_query("INSERT INTO `task` (`user_id`, `responsible_user_id`, `control_user_id`, `date`, `planned_end_date`, `fact_end_date`,`call_duration`,`priority_id`, `task_type_id`, `phone`, `comment`, `problem_comment`, `status`, `actived`)
						VALUES 
									('$user', '$persons_id', '$control_id', '$c_date', '$planned_start_date', '$fact_end_date', '$call_duration',  '$priority_id', '1', '$phone', '$comment', '$problem_comment', '0', '1');
	");
	
	
	if($rand_file != ''){
		mysql_query("INSERT INTO 	`file`
								( 	`user_id`,
								`task_id`,
								`name`,
								`rand_name`
								)
					VALUES
								(	'$user',
								'$hidden_inc',
								'$file',
								'$rand_file'
								);");
		
		
	}

}
function Addsite_user($incomming_call_id, $personal_pin, $friend_pin, $personal_id)
{
	
	$user		= $_SESSION['USERID'];
	mysql_query("INSERT INTO `site_user` 	(`incomming_call_id`, `site`, `pin`, `friend_pin`, `name`, `phone`, `mail`, `personal_id`, `user`)
						           		 VALUES 
											( '$incomming_call_id', '243', '$personal_pin', '$friend_pin', '3414', 12412341, '124124124', '$personal_id', '$user')");

}
				

function Savetask($task_id, $persons_id, $control_id, $planned_start_date, $fact_end_date,$call_duration,  $priority_id, $comment, $problem_comment,$rand_file, $file)
{
	global $error;
	$user  = $_SESSION['USERID'];
	$res_row = mysql_fetch_assoc(mysql_query("SELECT 	users.person_id
			FROM 	`users`
			WHERE 	`users`.`id` = $user"));
	
	
	
	
	
	if ($res_row[person_id]==$persons_id){
	
	$c_date		= date('Y-m-d H:i:s');
	
	
	mysql_query("UPDATE `task` SET  
								`responsible_user_id`	='$persons_id',
								`control_user_id` 		='$control_id',
								`date`					='$c_date',
								`planned_end_date`		='$planned_start_date',
								`fact_end_date`			='$fact_end_date', 
								`call_duration`			='$call_duration', 
								`priority_id`			='$priority_id',
								`comment`				='$comment', 
								`problem_comment`		='$problem_comment', 
								`status`='0', 
								`actived`='1'
								 WHERE `id`				='$task_id'
									");

		}else 
	{
		$error='თქვენ არ გაქვთ შეცვლის უფლება';
	}	
}


function Getcall_status($status)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `call_status`
						FROM 	`status`
						WHERE 	actived=1");
	

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		
		if($res['id'] == $call_status_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['$status'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['$status'] . '</option>';
		}
	}
	return $data;
}
	function Getpay_type($pay_type_id)
	{
		$data = '';
		$req = mysql_query("SELECT 	`id`, `name`
							FROM 	`pay_type`
							WHERE 	actived=1");
	
	
		$data .= '<option value="0" selected="selected">----</option>';
		while( $res = mysql_fetch_assoc($req)){
			if($res['id'] == $pay_type_id){
				$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
			} else {
				$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
			}
		}
	
		return $data;
	}
	function Get_bank($bank_id)
	{
		$data = '';
		$req = mysql_query("SELECT 	`id`, `name`
							FROM 	`bank`
							WHERE 	actived=1");
	
	
		$data .= '<option value="0" selected="selected">----</option>';
		while( $res = mysql_fetch_assoc($req)){
			if($res['id'] == $bank_id){
				$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
			} else {
				$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
			}
		}
	
		return $data;
	}
	
function Getbank_object($bank_object_id)
{  
	$data = '';
	$req = mysql_query("SELECT  id,
						     	`name`
						FROM 	bank_object
						WHERE 	bank_object.bank_id=$bank_object_id && actived =1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $bank_object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getbank_object_edit($bank_object_id)
{

	$data = '';
	$req = mysql_query("SELECT  id,
								`name`
						FROM 	bank_object
						WHERE 	bank_object.id=$bank_object_id && actived =1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $bank_object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
	
function Getcard_type($card_type_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
						FROM 	`card_type`
						WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $card_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getcard_type1($card_type1_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
						FROM 	`card_type`
						WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $card_type1_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getpay_aparat($pay_aparat_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
						FROM 	`pay_aparat`
						WHERE 	actived=1");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $pay_aparat_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getobject($object_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
						FROM 	`object`
						WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getcategory($category_id)

{ 			

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && parent_id=0 ");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getcategory1($category_id)
{

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && parent_id=$category_id");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
	
}

function Getcategory1_edit($category_id)
{

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && id=$category_id");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;

}

function Getcall_type($call_type_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `call_type`
						WHERE actived=1");
		
	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $call_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}


function Getdepartment($task_department_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					    FROM `department`
					    WHERE actived=1
						");
	

	
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $task_department_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getsource($object_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`source`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getpriority($priority_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `priority`
						WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $priority_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Get_control($persons_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `crystal_users`
							WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $persons_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Gettask_type($task_type_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					    FROM `task_type`
					    WHERE actived=1 AND id=1");

	
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $task_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}


function Getpersonss($persons_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `crystal_users`
							WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $persons_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function getCalls(){
	$db1 = new sql_db ( "212.72.155.176", "root", "Gl-1114", "asteriskcdrdb" );

	$req = mysql_query("

						SELECT  	DISTINCT
									IF(SUBSTR(cdr.src, 1, 3) = 995, SUBSTR(cdr.src, 4, 9), cdr.src) AS `src`
						FROM    	cdr
						GROUP BY 	cdr.src
						ORDER BY 	cdr.calldate DESC
						LIMIT 		12


						");

	$data = '<tr class="trClass">
					<th class="thClass">#</th>
					<th class="thClass">ნომერი</th>
					<th class="thClass">ქმედება</th>
				</tr>
			';
	$i	= 1;
	while( $res3 = mysql_fetch_assoc($req)){

		$data .= '
	    		<tr class="trClass">
					<td class="tdClass">' . $i . '</td>
					<td class="tdClass" style="width: 30px !important;">' . $res3['src'] . '</td>
					<td class="tdClass" style="font-size: 13px !important;"><button class="insert" number="' . $res3['src'] . '">დამატება</button></td>
				</tr>';
		$i++;
	}

	return $data;


}
function get_addition_all_info($pin)
{
	$res1 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pin = $pin AND incomming_call.call_type_id = 1 AND (task.`status` = 3 OR ISNULL(task.`status`))"));
	
	$res2 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a1`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pin = $pin AND incomming_call.call_type_id = 2 AND (task.`status` = 3 OR ISNULL(task.`status`))"));
	
	$res3 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a2`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pin = $pin AND incomming_call.call_type_id = 1 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));
	
	$res4 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a3`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pin = $pin AND incomming_call.call_type_id = 2 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));
	
	$sum_incomming	 	= $res1['a'] + $res2['a1'] + $res3['a2'] + $res4['a3'];
	$sum_end	 		= $res1['a'] + $res2['a1'];
	$sum_actived		= $res3['a2'] + $res4['a3'];
	
	$data .= '<fieldset>
					<legend>მომართვების ისტორია</legend>
					<table class="reqves_hist">
						<tr>
							<td>სულ</td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_incomming.'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოგვარებულები</td>
							<td></td>
							<td style="width: 150px;"></td>
						    <td>'.$sum_end.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრობლემა</td>
							<td style="width: 150px;"></td>
							<td>'. $res1['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'.$res2['a1'].'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოუგვარებელი</td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_actived.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრობლემა</td>
							<td style="width: 150px;"></td>
							<td>'.$res3['a2'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'. $res4['a3'].'</td>
						</tr>
					</table>
				</fieldset>';

	return $data;
}
function get_addition_all_info1($personal_id)
{
	$res1 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pid = $personal_id AND incomming_call.call_type_id = 1 AND (task.`status` = 3 OR ISNULL(task.`status`))"));

	$res2 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a1`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pid = $personal_id AND incomming_call.call_type_id = 2 AND (task.`status` = 3 OR ISNULL(task.`status`))"));

	$res3 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a2`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pid = $personal_id AND incomming_call.call_type_id = 1 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));

	$res4 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a3`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pid = $personal_id AND incomming_call.call_type_id = 2 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));

	$sum_incomming	 	= $res1['a'] + $res2['a1'] + $res3['a2'] + $res4['a3'];
	$sum_end	 		= $res1['a'] + $res2['a1'];
	$sum_actived		= $res3['a2'] + $res4['a3'];

	$data .= '<fieldset>
					<legend>მომართვების ისტორია</legend>
					<table class="reqves_hist">
						<tr>
							<td>სულ</td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_incomming.'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოგვარებულები</td>
							<td></td>
							<td style="width: 150px;"></td>
						    <td>'.$sum_end.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრობლემა</td>
							<td style="width: 150px;"></td>
							<td>'. $res1['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'.$res2['a1'].'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოუგვარებელი</td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_actived.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრობლემა</td>
							<td style="width: 150px;"></td>
							<td>'.$res3['a2'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'. $res4['a3'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
				    		<td style="width: 300px;">
							<input type="button" value="ვრცლად"/>
		      				</td>
						</tr>
					</table>
				</fieldset>';

	return $data;
}
function Getincomming($task_id)
{
$res = mysql_fetch_assoc(mysql_query("	SELECT		task.id AS `id`,
													incomming_call.id AS `call_id`,
													incomming_call.uniqueid as inc_uniqueid,
													task.uniqueid as task_uniqueid,
													IF(ISNULL(task.incomming_call_id), task.phone, incomming_call.phone) AS task_phone,
													IF(ISNULL(task.incomming_call_id), DATE_FORMAT(task.`date`,'%d/%m/%y %H:%i'), DATE_FORMAT(incomming_call.`date`,'%d/%m/%y %H:%i')) AS call_date,
													DATE_FORMAT(incomming_call.`date`,'%Y-%m-%d %H') AS record_date,
													incomming_call.call_type_id AS call_type_id,
													incomming_call.call_category_id AS category_id,
													IF(ISNULL(task.`status`), 3, task.`status`) AS `status`,
													incomming_call.call_subcategory_id AS category_parent_id,
													DATE_FORMAT(incomming_call.`problem_date`,'%d/%m/%y %H:%i') AS problem_date,
													incomming_call.call_content AS call_content,
													incomming_call.pay_type_id AS pay_type_id,
													incomming_call.bank_id AS bank_id,
													incomming_call.bank_object_id AS bank_object_id,
													incomming_call.card_type_id AS card_type_id,
													incomming_call.card_type_id AS card_type1_id,
													incomming_call.pay_aparat_id AS pay_aparat_id,
													incomming_call.object_id AS object_id,
													incomming_call.pid AS personal_id,
													incomming_call.pin AS personal_pin,
													task.task_type_id AS task_type_id,
													task.responsible_user_id AS persons_id,
													task.priority_id AS priority_id,
													DATE_FORMAT(task.`planned_end_date`,'%d/%m/%y %H:%i:%s') as planned_end_date,
													DATE_FORMAT(task.fact_end_date,'%d/%m/%y %H:%i:%s') as fact_end_date,
													CONCAT(DATEDIFF(task.`fact_end_date`, task.`date`),' ','დღე და',' ',TIMEDIFF(DATE_FORMAT(task.`fact_end_date`,'%H:%i:%s'),DATE_FORMAT(task.`date`,'%H:%i:%s')),' ', 'სთ') as duration,
													task.department_id AS task_department_id,
													task.phone AS phone,
													task.`comment` AS `comment`,
													task.control_user_id,
													task.problem_comment AS problem_comment
													

										FROM 	   	task
										LEFT JOIN  	incomming_call  ON incomming_call.id = task.incomming_call_id
										WHERE      	task.id = $task_id
			" ));
	
	return $res;
}


function GetPage($res='', $number)
{
	$num = 0;
	if($res[phone]==""){
		$num=$number;
	}else{ 
		$num=$res[phone]; 
	}
	
	$increm = mysql_query("	SELECT  `name`,
			`rand_name`
			FROM 	`file`
			WHERE   `task_id` = $res[id]
			");
	
	if ($res[call_id] == '') {
		
		$data  .= '<div id="dialog-form">
							<div style="float: left; width: 500px;">
								<fieldset >
							    	<legend>ძირითადი ინფორმაცია</legend>
										<table width="100%" class="dialog-form-table">
										<tr>
											<td style="width: 180px;"><label for="req_num">დავალების №</label></td>
											<td style="width: 180px !important;"><label for="req_data">ფორმირების თარიღი</label></td>
											<td style="width: 180px;"><label for="req_phone">ტელეფონი</label></td>
										</tr>
										<tr>
											<td style="width: 180px;">
												<input type="text" id="id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField user_id\'" value="' . $res['id']. '" disabled="disabled" />
											</td>
											<td style="width: 180px;">
												<input type="text" id="c_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField date\'" value="' .  $res['call_date']. '" disabled="disabled" />
											</td>
											<td style="width: 180px;">
												<input type="text" id="task_phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' .  $res['task_phone'] . '" />
											</td>
											<td style="width: 69px;">
												
											</td>
										</tr>
										<tr>
											<td style="width: 215px;"><label for="req_num">შესრ.პერ.დასაწყისი</label></td>
											<td style="width: 215px !important;"><label for="req_data">შესრ.პერ.დასასრული</label></td>
											<td style="width: 215px;"><label for="req_phone">შესრულების დრო</label></td>
										</tr>
										<tr>
											<td style="width: 215px;">
												<input type="text" id="planned_date" style="width: 180px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' .  $res['call_date']. '"disabled="disabled" />
											</td>
											<td style="width: 215px;">
												<input type="text" id="fact_end_date" style="width: 180px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' .  $res['fact_end_date']. '"/>
											</td>
											<td style="width: 215px;">
												<input type="text" id="call_duration" style="width: 180px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['duration'] . '"disabled="disabled" />
											</td>
										</tr>
								</fieldset>';
				
						$data  .= '<table class="dialog-form-table">
										<tr>
											<td style="width: 180px;"><label for="d_number">დავალების ტიპი</label></td>
											<td style="width: 180px;"><label for="d_number">განყოფილება</label></td>
											<td style="width: 180px;"><label for="d_number">პასუხისმგებელი პირი</label></td>
											
										</tr>
							    		<tr>
											<td style="width: 180px;"><select id="task_type_id" class="idls object" disabled="disabled">'.Gettask_type($res['task_type_id']).'</select></td>
											<td style="width: 180px;"><select id="task_department_id" class="idls object"disabled="disabled">'. Getdepartment($res['task_department_id']).'</select></td>
											<td style="width: 180px;"><select id="persons_id" class="idls object">'.Getpersonss($res['persons_id']).'</select></td>
											
										</tr>
										<tr>
											<td style="width: 180px;"><label for="d_number">პრიორიტეტები</label></td>
											<td style="width: 180px;"><label for="d_number">მაკონტროლებელი</label></td>
											<td style="width: 180px;"></td>
										</tr>
										<tr>
											<td style="width: 180px;"><select id="priority_id" class="idls object">'.Getpriority($res['priority_id']).'</select></td>
											<td style="width: 180px;"><select  id="control_id" class="idls object">'.Get_control($res['control_user_id']).'</select></td>
											<td style="width: 180px;"></td>
										</tr>
										<tr>
											<td style="width: 150px;"><label for="content">კომენტარი</label></td>
											<td style="width: 150px;"><label for="content"></label></td>
											<td style="width: 150px;"><label for="content"></label></td>
										</tr>
													
										
										<tr>
						
											<td colspan="6">
												<textarea  style="width: 641px; resize: none;" id="comment" class="idle" name="call_content" cols="300" rows="2">' . $res['comment'] . '</textarea>
											</td>
										</tr>
										<tr>
											<td style="width: 180px;"><label for="content">საუბრის შინაარსი</label></td>
										</tr>
										<tr>
								
											<td colspan="6">
												<textarea style="width: 641px; resize: none;" id="problem_comment" class="idle" name="call_content" cols="300" rows="2" >' . $res['problem_comment'] . '</textarea>
											</td>
										</tr>
									</table>
						        </fieldset>
							</div>
							<div>
								  </fieldset>
							</div>
							<div style="float: right;  width: 355px;">
								 <fieldset>
									<legend>მომართვის ავტორი</legend>
									<table style="height: 133px;">
										<tr>
											<td style="width: 180px;">PIN კოდი</td>
											<td style="width: 180px;"></td>
										</tr>
										<tr>
											<td style="width: 180px;"><input type="text" id="personal_pin" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['personal_pin']  . '" /></td>
											<td style="width: 180px;" class="hidden friend">
											</td>
										</tr>
										<tr>
											<td style="width: 180px;">პირადი ნომერი</td>
											<td style="width: 180px;"></td>
										</tr>
										<tr>
											<td style="width: 180px;">
												<input type="text" id="personal_id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['personal_id'] . '" />
											</td>
											<td style="width: 180px;"></td>
										</tr>
									</table>
								</fieldset>
								<div id="additional_info">';
					if (!empty($res['personal_pin'])){
							$data .= get_addition_all_info($res['personal_pin']);
						}
						$data .= GetRecordingsSection1($res);
						$data .= GetRecordingsSection($res);
	  $data .= '</div>
								<fieldset>
									<table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
										<tr>
											<td>
												<div class="file-uploader">
													<input id="choose_file" type="file" name="choose_file" class="input" style="display: none;">
													<button id="choose_button" class="center">აირჩიეთ ფაილი</button>
													<input id="hidden_inc" type="text" value="'. increment('task') .'" style="display: none;">
												</div>
											</td>
										</tr>
									</table>
								     <table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
								          <tr style="border-bottom: 1px solid #85b1de;">
								           <td colspan="3">მიმაგრებული ფაილი</td>
								          </tr>
									</table>
									<table id="file_div" style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">';
										
										while($increm_row = mysql_fetch_assoc($increm))	{	
											$data .=' 
													        <tr style="border-bottom: 1px solid #85b1de;">
													          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>													 
													          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
													          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
													        </tr>';
										}
								         
						 $data .= '
				 					</table>
								</fieldset>
							</div>
				    </div>';
	}else {
		$data  .= '<div id="dialog-form">
					<div style="float: left; width: 500px;">	
						<fieldset >
					    	<legend>ძირითადი ინფორმაცია</legend>
				
					    	<table width="100%" class="dialog-form-table">
								<tr>
									<td style="width: 180px;"><label for="req_num">მომართვა №</label></td>
									<td style="width: 180px !important;"><label for="req_data">თარიღი</label></td>
									<td style="width: 180px;"><label for="req_phone">ტელეფონი</label></td>
								</tr>
								<tr>
									<td style="width: 180px;">
										<input type="text" id="id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField user_id\'" value="' . $res['id']. '" disabled="disabled" />
									</td>
									<td style="width: 180px;">
										<input type="text" id="c_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField date\'" value="' .  $res['call_date']. '" disabled="disabled" />
									</td>
									<td style="width: 180px;">
										<input type="text" id="phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' .  $res['task_phone']. '"disabled="disabled" />
									</td>
									<td style="width: 69px;">
										<button class="calls">ნომრები</button>
									</td>
								</tr>
								<tr>
									<td style="width: 180px;"><label for="d_number">ზარის ტიპი</label></td>
									<td style="width: 180px;"><label for="d_number">კატეგორია</label></td>
									<td style="width: 180px;"><label for="d_number">ქვე-კატეგოტია</label></td>
									<td ></td>
								</tr>
								<tr>
									<td style="width: 180px;"><select id="call_type_id" class="idls object" disabled="disabled">'. Getcall_type($res['call_type_id']).'</select></td>
									<td style="width: 180px;"><select id="category_parent_id" class="idls object" disabled="disabled">'. Getcategory($res['category_parent_id']).'</select></td>
									<td style="width: 180px;"><select id="category_id" class="idls object" disabled="disabled">'. Getcategory1_edit($res['category_id']).'</select></td>
									<td ></td>
								</tr>
							</table>';
				if (Getcategory($res['category_parent_id'])==407) {
					$data  .= '<table id="additional" class="hidden dialog-form-table" width="100%">
								<tr >
									<td style="width: 180px;"><label for="d_number">ტრანზაქციის ფორმა</label></td>
									<td style="width: 180px;"><label for="d_number">მომსახურე ბანკი</label></td>
									<td style="width: 180px;"><label for="d_number">ფილიალი</label></td>
									<td style="width: 106px;"></td>
								</tr>
								<tr >
									<td style="width: 180px;"><select id="pay_type_id" class="idls object" disabled="disabled">'. Getpay_type($res['pay_type_id']).'</select></td>
									<td style="width: 180px;"><select id="bank_id" class="idls object" disabled="disabled">'. Get_bank($res['bank_id']).'</select></td>
									<td style="width: 180px;"><select id="bank_object_id" class="idls object" disabled="disabled">'. Getbank_object_edit($res['bank_object_id']).'</select></td>
									<td style="width: 106px;"></td>
								</tr>
								<tr >
									<td style="width: 180px;"><label for="d_number">ბარათის ტიპი</label></td>
									<td style="width: 180px;"><label for="d_number">ანგარიშსწორება</label></td>
									<td style="width: 180px;"><label for="d_number">აპარატის ტიპი</label></td>
									<td style="width: 106px;"></td>
								</tr>
								<tr >
									<td style="width: 180px;"><select id="card_type_id" class="idls object" disabled="disabled">'. Getcard_type($res['card_type_id']).'</select></td>
									<td style="width: 180px;"><select id="card_type1_id" class="idls object" disabled="disabled">'. Getcard_type1($res['card_type1_id']).'</select></td>
									<td style="width: 180px;"><select id="pay_aparat_id" class="idls object" disabled="disabled">'. Getpay_aparat($res['pay_aparat_id']).'</select></td>
									<td style="width: 106px;"></td>
								</tr>
							</table>';
				}								
				
														
				$data  .= '<table width="100%" class="dialog-form-table">
								<tr>
									<td style="width: 180px;"><label for="req_num">პრობლემის თარიღი</label></td>
									<td style="width: 180px;"><label for="d_number">ობიექტი</label></td>
									<td style="width: 180px;"><label for="d_number">სტატუსი</label></td>			
									<td ></td>
								</tr>
								<tr>	
									<td style="width: 180px;"> <input type="text" id="problem_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res[problem_date] . '"disabled="disabled" /></td>
									<td style="width: 180px;"><select id="object_id" class="idls object" disabled="disabled">'. Getobject($res['object_id']).'</select></td>
									<td style="width: 180px;"><select id="call_status_id" class="idls object"disabled="disabled">'. Getcall_status($res['status']).'</select></td>	
									<td ></td>
								</tr>
								<tr>
									<td style="width: 180px;"><label for="content">საუბრის შინაარსი</label></td>
								</tr>
								<tr>
									
									<td colspan="6">	
										<textarea disabled="disabled" style="width: 641px; resize: none;" id="call_content" class="idle" name="call_content" cols="300" rows="2" >' . $res['call_content'] . '</textarea>
									</td>
								</tr>	
								
							</table>
						</fieldset >
				   
						<fieldset style="margin-top: 5px;">
					    	<legend>დავალების ფორმირება</legend>
				
					    	<table class="dialog-form-table">
								<tr>
									<td style="width: 180px;"><label for="d_number">დავალების ტიპი</label></td>
									<td style="width: 180px;"><label for="d_number">განყოფილება</label></td>
									<td style="width: 180px;"><label for="d_number">პასუხისმგებელი პირი</label></td>
								</tr>
					    		<tr>
									<td style="width: 180px;"><select id="task_type_id" class="idls object"disabled="disabled">'.Gettask_type($res['task_type_id']).'</select></td>
									<td style="width: 180px;"><select id="task_department_id" class="idls object"disabled="disabled">'. Getdepartment($res['task_department_id']).'</select></td>
									<td style="width: 180px;"><select id="persons_id" class="idls object"disabled="disabled">'.Getpersonss($res['persons_id']).'</select></td>
								</tr>
								<tr>
									<td style="width: 215px;"><label for="task_type_id">ინფორმაციის წყარო</label></td>
									<td style="width: 215px;"><label for="d_number">სხვა ნომერი</label></td>
									<td style="width: 215px;"><label for="d_number">მაკონტროლებელი</label></td>
								</tr>
								<tr>
									<td style="width: 215px;"><select id="source" class="idls object"disabled="disabled">'.Getsource($res['source']).'</select></td>
									<td style="width: 180px;"><input type="text" id="phone_out" class="idle" onblur="this.className=\'idle\'" value="' . $res['phone'] . '"disabled="disabled" /></td>
									<td style="width: 215px;"><select  id="control_id" class="idls object"disabled="disabled">'.Get_control($res['control_user_id']).'</select></td>
								</tr>
								<tr>
									<td style="width: 180px;"><label for="content">პრობლემის გადაწყვეტა</label></td>
								</tr>
								<tr>
						
									<td colspan="6">
										<textarea  style="width: 641px; resize: none;" id="content1" class="idle" name="call_content" cols="300" rows="2">' . $res['problem_comment'] . '</textarea>
									</td>
								</tr>		
							
								</tr>
							</table>
				        </fieldset>
					</div>
					<div style="float: right;  width: 355px;">
						<fieldset>
									<legend>მომართვის ავტორი</legend>
									<table style="height: 133px;">
										<tr>
											<td style="width: 180px;">PIN კოდი</td>
											<td style="width: 180px;"></td>
										</tr>
										<tr>
											<td style="width: 180px;"><input type="text" id="personal_pin" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['personal_pin']  . '" /></td>
											<td style="width: 180px;" class="hidden friend">
											
										    </td>
										</tr>
										<tr>
											<td style="width: 180px;">პირადი ნომერი</td>
											<td style="width: 180px;"></td>
										</tr>
										<tr>
											<td style="width: 180px;">
												<input type="text" id="personal_id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['personal_id'] . '" />
											</td>
											<td style="width: 180px;"></td>
										</tr>
									</table>
								</fieldset>
						<div id="additional_info">';
					if (!empty($res['personal_pin'])){
							$data .= get_addition_all_info($res['personal_pin']);
						}
					$data .= GetRecordingsSection1($res);
					$data .= GetRecordingsSection($res);
	
	 $data .= '		<fieldset>
						<legend>დამატებითი ინფორმაცია</legend> 
						<table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
						<tr>
							<td>
								<div class="file-uploader">
									<input id="choose_file" type="file" name="choose_file" class="input" style="display: none;">
									<button id="choose_button" class="download">აირჩიეთ ფაილი</button>
									<input id="hidden_inc" type="text" value="'. increment('task') .'" style="display: none;">
								</div>
							</td>
						</tr>
					</table>
				     <table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
				          <tr style="border-bottom: 1px solid #85b1de;">
				           <td colspan="3">მიმაგრებული ფაილი</td>
				          </tr>
					</table>
					<table id="file_div" style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">';
						
						while($increm_row = mysql_fetch_assoc($increm))	{	
							$data .=' 
									        <tr style="border-bottom: 1px solid #85b1de;">
									          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>													 
									          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
									          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
									        </tr>';
						}
				         
		 $data .= '
 					</table>
					</fieldset>
				</div>
		    </div>';	
	}
	
	$data .= '<input type="hidden" id="outgoing_call_id" value="' . $res['id'] . '" />';

	return $data;
}
function GetRecordingsSection($res)
{
	if ($res[task_phone]==''){
		//$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}else{
	$req = mysql_query("SELECT  	DATE_FORMAT(asterisk_incomming.call_datetime,'%d/%m/%y %H:%i:%s') AS `time`,
									asterisk_incomming.file_name AS `userfield`
						FROM 		asterisk_incomming
						WHERE		asterisk_incomming.uniqueid ='$res[inc_uniqueid]'");
	}
	$data .= '
        <fieldset style="margin-top: 0px; width: 155px; float: right;">
            <legend>შემომავალი ზარი</legend>

            <table style="width: 65%; border: solid 1px #85b1de; margin:auto;">
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <th style="padding-left: 10px;">დრო</th>
                    <th  style="border: solid 1px #85b1de; padding-left: 10px;">ჩანაწერი</th>
                </tr>';
	if (mysql_num_rows($req) == 0){
		$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}
	
	while( $res2 = mysql_fetch_assoc($req)){
		$src = $res2['userfield'];
		$data .= '
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <td>' . $res2['time'] . '</td>
                    <td><button class="download" str="' . $src . '">შემომავალი</button></td>
                </tr>';
	}

	$data .= '
            </table>
        </fieldset>';
	
	return $data;
}
function GetRecordingsSection1($res)
{
	if ($res[task_phone]==''){
		
	}else{
		$req = mysql_query("SELECT  DATE_FORMAT(asterisk_outgoing.call_datetime,'%d/%m/%y %H:%i:%s') AS `time`,
									asterisk_outgoing.file_name AS `userfield`
							FROM 	asterisk_outgoing
							WHERE 	asterisk_outgoing.uniqueid='$res[task_uniqueid]'");
	}
	$data .= '
        <fieldset style="margin-top: 0px; width: 154px; float: right;">
            <legend>გამავალი ზარი</legend>

            <table style="width: 65%; border: solid 1px #85b1de; margin:auto;">
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <th style="padding-left: 10px;">დრო</th>
                    <th  style="border: solid 1px #85b1de; padding-left: 10px;">ჩანაწერი</th>
                </tr>';
	if (mysql_num_rows($req) == 0){
		$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}

	while( $res2 = mysql_fetch_assoc($req)){
		$src = $res2['userfield'];
		$data .= '
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <td>' . $res2['time'] . '</td>
                    <td><button class="download1" str="' . $src . '">გამავალი</button></td>
                </tr>';
	}

	$data .= '
            </table>
        </fieldset>';

	return $data;
}
function ChangeResponsiblePerson($letters, $responsible_person){
	
	$o_date		= date('Y-m-d H:i:s');

	
	foreach($letters as $letter) {
		
		$res = mysql_fetch_assoc(mysql_query("	SELECT 	task.`status`
				FROM 	task
				WHERE 	id='$letter'
				"));
		$filter=$res['status'];
		if ($filter==2){	
		mysql_query("UPDATE task
					SET    	task.`status`   			 = 1,
							task.`date` 			     = '$o_date',
							task.responsible_user_id1     = '$responsible_person'
					WHERE  	task.id 					 = '$letter'");
		}else 
		{
			mysql_query("UPDATE task
			SET    	task.`status`   			 = 1,
			task.`date` 			     = '$o_date',
			task.responsible_user_id1     = '$responsible_person'
			WHERE  	task.id 					 = '$letter'");
		}
	}
}

function GetPersons(){
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `crystal_users`
							WHERE actived=1");

	$data .= '<option value="' . 0 . '" selected="selected">' . '' . '</option>';

	while( $res = mysql_fetch_assoc($req)){
		$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
	}
	return $data;
}
function GetResoniblePersonPage(){
	$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ძირითადი ინფორმაცია</legend>
				<table width="100%" class="dialog-form-table" cellpadding="10px" >
					<tr>
						<th><label for="responsible_person">პასუხისმგებელი პირი</label></th>
					</tr>
					<tr>
						<th>
							<select id="responsible_person" class="idls address">'. GetPersons() .'</select>
						</th>
					</tr>
				</table>
			</fieldset>
		</div>';
	return $data;

}
function increment($table){

	$result   		= mysql_query("SHOW TABLE STATUS LIKE '$table'");
	$row   			= mysql_fetch_array($result);
	$increment   	= $row['Auto_increment'];
	$next_increment = $increment+1;
	mysql_query("ALTER TABLE '$table' AUTO_INCREMENT=$next_increment");

	return $increment;
}
?>